package com.czl.module_storehouse.activity.allocate.post;

import android.os.Bundle;
import android.view.View;

import com.czl.base.data.bean.tengyun.ProductBean;
import com.czl.module_base.http.HttpResponse;
import com.czl.module_base.mvp.presenter.InjectPresenter;
import com.czl.module_storehouse.activity.remand.scan.RemandScanActivity;
import com.czl.module_storehouse.event.AllocatePostInSortEvent;
import com.czl.module_storehouse.mvp.presenter.ProductPresenter;
import com.czl.module_storehouse.mvp.presenter.StorehouseLocationPresenter;
import com.czl.module_storehouse.mvp.view.StorehouseLocationView;
import com.jeremyliao.liveeventbus.LiveEventBus;

import java.util.List;

/**
 * @author xubin
 */
public class AllocateScanActivity extends RemandScanActivity implements StorehouseLocationView {

    private List<ProductBean> mMoveProductViewList;
    @InjectPresenter
    StorehouseLocationPresenter mStorehouseLocationPresenter;
    @InjectPresenter
    ProductPresenter mProductPresenter;

    @Override
    protected StorehouseLocationPresenter getLocationPresenter() {
        return mStorehouseLocationPresenter;
    }

    @Override
    protected ProductPresenter getProductPresenter() {
        return mProductPresenter;
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        mToolBinding.toolbarContentTitle.setText("物品调拨");
        binding.headerRemand.textScan.setText("扫码调拨");
        binding.textDamage.setText("扫码调拨");
        binding.textReturn.setText("确定");
        binding.headerRemand.imageView.setVisibility(View.GONE);
        binding.headerRemand.tvTitleNum.setVisibility(View.GONE);
        LiveEventBus.get(AllocatePostInSortEvent.class).observe(this, event -> {
            mSortBean = event.getSortBean();
            initDataAtSort(mSortBean);
            if (event.getAllocate() != null) {
                mMoveProductViewList = event.getAllocate().getMoveProductViewList();
            }
        });
    }

    @Override
    protected boolean interruptInitData() {
        return true;
    }

    @Override
    protected boolean setResultProduct(ProductBean productBean) {
        if (mSortBean != null) {
            if (!productBean.isSortProduct(mSortBean.getSortId())) {
                showToast("不是该调拨物品");
                return true;
            }
        }
        if (mMoveProductViewList != null) {
            ProductBean allocateProduct = productBean.isAllocateProduct(mMoveProductViewList);
            if (allocateProduct == null) {
                showToast("该物品不是调拨物品");
                return true;
            } else if (allocateProduct.getInStorehouseLocatId() != null && allocateProduct.getInStorehouseLocatId() > 0) {
                showToast("物品已调入该仓库");
                return true;
            }
        }

        return false;
    }

    @Override
    protected boolean isTextEnable() {
        return true;
    }

    @Override
    protected void getProductUseInfoByCode(String productCode) {
        if (mMoveProductViewList != null) {
            ProductBean allocateProduct = isAllocateProductByCode(mMoveProductViewList, productCode);
            if (allocateProduct == null) {
                showToast("该物品不是调拨物品");
                return;
            } else if (allocateProduct.getInStorehouseLocatId() != null && allocateProduct.getInStorehouseLocatId() > 0) {
                showToast("物品已调入该仓库");
                return;
            }
        }
        getProductPresenter().getProductInfoByCode(productCode);
    }

    /**
     * 判断是否是待调拨物品
     *
     * @param list
     * @param productCode
     * @return
     */
    public ProductBean isAllocateProductByCode(List<ProductBean> list, String productCode) {
        if (list == null) {
            return null;
        }
        for (ProductBean productBean : list) {
            if (productBean.getProductCode() == null) {
                continue;
            }
            if (productBean.getProductCode().equals(productCode)) {
                return productBean;
            }
        }
        return null;
    }

    @Override
    public <T> void showHttpResponse(HttpResponse<T> httpResponse) {
        super.showHttpResponse(httpResponse);
        if (ProductPresenter.TAG_PRODUCT_INFO_BY_CODE.equals(httpResponse.getRequestTag())) {
            ProductBean productBean = (ProductBean) httpResponse.getData();
            if (productBean == null) {
                showToast("物品信息错误");
                return;
            }
            if (setResultProduct(productBean)) {
                return;
            }

            boolean add = mSelectProductIds.add(productBean.getProductId());
            if (add) {
                productBean.setManufactureDate(null);
                mAdapter.add(productBean, mAdapter.getSelectGroupNode());
                binding.textReturn.setEnabled(true);
            } else {
                showToast("该物品已添加");
            }
        }
    }
}
